package org.example.autoactivationplugin;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.lang.IgnitePredicate;

/**
 * Activate cluser when nodes whith specified ConsistentID values join topology.
 * 
 * @author user
 */
public class ActivateByConstistendID implements IgnitePredicate {

    private Set<String> requredNodes;

    /**
     * Constructor
     * @param requredNodes List of ConsistentIDs
     */
    public ActivateByConstistendID(Set<String> requredNodes) {
        if (requredNodes == null || requredNodes.isEmpty()) {
            throw new IllegalArgumentException("requiredNodes must be set");
        }
        this.requredNodes = requredNodes;
    }
    
    @Override
    public boolean apply(Object e) {
        Collection<ClusterNode> nodes = (Collection<ClusterNode>) e;
        Set<String> missingNodes = new LinkedHashSet<>(requredNodes);
        for (ClusterNode node : nodes) {
            String nodeConsistentId = node.consistentId().toString();
            missingNodes.remove(nodeConsistentId);
            if (missingNodes.isEmpty()) {
                break;
            }
        }
        return missingNodes.isEmpty();
    }

}
